<?php
 
function report_calon_karyawan(){
	my_set_code_js('
		function confirmDelete(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com=pelamar&task=delete&id=\'+i;
		}
		return;	}
	');
	$header = array(
	 	'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'pic'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'usia'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'no agenda'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),
		'Tgl Lamaran'=>array('style'=>'text-align:center;border-bottom:2px solid;width:13%'),
		'daerah asal'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:12%'), 
	);
	if(isset($_GET['key'])){
		$key = rawurldecode(trim($_GET['key']));
		$query = "
		SELECT a.pelamar_id,b.nama,b.tanggal_lahir,a.nomor_agenda,a.tanggal_terima,d.kota FROM pelamar a 
			INNER JOIN biodata b ON a.biodata_id=b.biodata_id
			LEFT JOIN pelamar_alamat c on c.pelamar_id=a.pelamar_id
			LEFT JOIN kodepos d ON d.kodepos_id=c.kodepos_id
			WHERE ( a.nomor_agenda LIKE '%{$key}%' OR b.nama LIKE '%{$key}%')
			AND a.pelamar_id NOT IN (SELECT pelamar_id FROM pelamar_seleksi)
			ORDER BY a.pelamar_id DESC
		";
	}else{
		$query = "
		SELECT a.pelamar_id,b.nama,b.tanggal_lahir,a.nomor_agenda,a.tanggal_terima,d.kota FROM pelamar a 
			INNER JOIN biodata b ON a.biodata_id=b.biodata_id
			LEFT JOIN pelamar_alamat c on c.pelamar_id=a.pelamar_id
			LEFT JOIN kodepos d ON d.kodepos_id=c.kodepos_id
			WHERE a.pelamar_id NOT IN (SELECT pelamar_id FROM pelamar_seleksi)
			ORDER BY a.pelamar_id DESC
		";
	}

	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=pelamar&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;
		$editproperty = array(
				'href'=>'index.php?com=pelamar&task=edit&id='.$ey['pelamar_id'], 
				'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
				'href'=>'index.php?com=pelamar&task=infopelamar&pelamar_id='.$ey['pelamar_id'], 
				'title'=>'Edit'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
				'href'=>'javascript:;',
				'onclick'=>'javascript:confirmDelete('.$ey['pelamar_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			
		$form = '<input type="checkbox" name="a['.$ey['pelamar_id'].']" value="1"/>';
		$row[] = array(
		 	'#'=>position_text_align ($form, 'center'),
			'photo'=>position_text_align ('<img src="files/upload/sample_thumb.jpg" height="30" width="30" />', 'center'),
			'nama'=>$ey['nama'], 
			'usia'=>position_text_align(  getage( $ey['tanggal_lahir']  ) , 'center'),
			'no peserta'=>position_text_align( $ey['nomor_agenda'], 'center'), 
			'tgl'=>position_text_align( date('d- m - Y' , strtotime($ey['tanggal_terima']  )), 'center'), 
			'asal'=> ucfirst($ey['kota']),  
			//'pendidikan'=>position_text_align ($ey['pendidikan'],'center'), 
			'operasi'=> position_text_align(  $edit_button.' '. $detail_button.' '.$delete_button , 'right'),  
		);
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	
	$menuontop = array(   
		'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=pelamar&task=edit&id=0\''),
		'Pilih'=>array('onclick'=>'javascript:document.f.submit()'),
	);
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana">
	<b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search); 
	return $printmenu . '<form method="POST" name="f">'.table_builder($header , $datas , 8 ,false,$paging ).'</form>';
}


function set_pelamar_test($datas){  //tombol pilih
	foreach($datas as $key=>$data){
		$res = array(
			'pelamar_id'=>my_type_data_int($key),
			'seleksi_pelamar_id'=>my_type_data_int(1),
			'tanggal_panggil'=>my_type_data_function('DATE(NOW())'),
		);
		my_insert_record('pelamar_seleksi',$res);
	}
	my_direct('index.php?com=pelamar');
} 

function form_create($id =0){
	my_set_file_js(array(
		'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
		'components/system/js/calendar/calendarDateInput.js'));	 

	my_set_file_css(
			array(
					'components/system/jquery/autocomplete/jquery.autocomplete.css' 
				)
		);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0];
		}

		$(document).ready(function() {
			$("#propinsi").autocomplete(
				"autocomplete_daftar_propinsi.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function findValueKota(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItemKota(li) {
			findValueKota(li);
		}

		function formatItemKota(row) {
			return   row[0];
		}  
		 
		 
			
		$(document).ready(function() {
			$("#kota").autocomplete(
				"autocomplete_daftar_kota.php",
				{
					delay:200,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItemKota,
					onFindValue:findValueKota,
					formatItem:formatItemKota,  
					autoFill:true,
					extraParams: {propinsi: function(){ return $("#propinsi").val(); } }
				}
			);
			 
		});

		function findValueKecamatan(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItemKecamatan(li) {
			findValueKota(li);
		}

		function formatItemKecamatan(row) {
			return   row[0];
		} 

		$(document).ready(function() {
			$("#kecamatan").autocomplete(
				"autocomplete_daftar_kecamatan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItemKecamatan,
					onFindValue:findValueKecamatan,
					formatItem:formatItemKecamatan, 
					autoFill:true,
					extraParams: {kota: function() { return $("#kota").val(); } }
				}
			);
			 
		});

		function findValueKelurahan(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItemKelurahan(li) {
			findValueKota(li);
		}

		function formatItemKelurahan(row) {
			return   row[0];
		} 

		$(document).ready(function() {
			$("#kelurahan").autocomplete(
				"autocomplete_daftar_kelurahan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItemKelurahan,
					onFindValue:findValueKelurahan,
					formatItem:formatItemKelurahan, 
					autoFill:true,
					extraParams: {kecamatan: function(){ return $("#kecamatan").val(); } }
				}
			);
			 
		});
		function findValueKodePos(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItemKodePos(li) {
			findValueKota(li);
		}

		function formatItemKodePos(row) {
			return   row[0];
		} 

		$(document).ready(function() {
			$("#kodepos").autocomplete(
				"autocomplete_daftar_kodepos.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItemKodePos,
					onFindValue:findValueKodePos,
					formatItem:formatItemKodePos, 
					autoFill:true,
					extraParams: {kelurahan: function(){ return $("#kelurahan").val(); } }
				}
			);
			 
		});
	');
	
	
	$fields =  loaddata_pelamar($id);
	if(!$fields)
		$nomorbaru = nomer_pelamar_baru();
	else
		$nomorbaru = $fields['nomor_agenda'];
	
	$view = form_header( "calon karyawan" , "cp"  ); 
	$nomor = array(
				'name'=>'nomorbaru',
				'value'=>$nomorbaru,
				'id'=>'nomorbaru',
				'type'=>'hidden',
				'size'=>'10', 
			);
	$form_field_no= form_dynamic($nomor);
	$view .= form_field_display( $nomorbaru .$form_field_no , "Nomor agenda"    ); 
	
	$tanggal_terima = array(
			'name'=>'tanggal_terima',
			'value'=>( isset($_POST['tanggal_terima']) ? $_POST['tanggal_terima'] : date('d-m-Y') ),
			'id'=>'tanggal_terima',
			'type'=>'text','size'=>'55' 
		);
	$form_field_tanggal_lahir = form_calendar ($tanggal_terima);
	$view .= form_field_display( $form_field_tanggal_lahir   , "Tanggal terima surat"    );
	
	$nama = array(
				'name'=>'nama',
				'value'=>( isset($_POST['nama']) ? $_POST['nama'] : $fields['nama'] ),
				'id'=>'nama',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	
	$view .= form_field_display( $form_field_nama   , "Nama"   );
	
	
	$email = array(
				'name'=>'email',
				'value'=>( isset($_POST['email']) ? $_POST['email'] : $fields['email'] ),
				'id'=>'email',
				'type'=>'text','size'=>'35' 
			);
	$form_field_email = form_dynamic($email);
	$view .= form_field_display( $form_field_email   , "Email"    );
	list($tno,$numberid)=explode('#',$fields['idcard']);
	$types= array(
			'KTP'=>'KTP',
			'SIM'=>'SIM',
			'PASSPORT'=>'PASSPORT',
			'KITAS'=>'KITAS',
		);
	$type = array(
			'name'=>'tno',
			'id'=>'tno',
			'value'=>isset($_POST['tno']) ? $_POST['tno'] :$tno,
	
		);
	$form_tpye = form_dropdown( $type , $types );
	$idcard = array(
				'name'=>'idcard',
				'value'=>( isset($_POST['idcard']) ? $_POST['idcard'] : $numberid ),
				'id'=>'idcard',
				'type'=>'text','size'=>'35' 
			);
	$form_field_idcard = form_dynamic($idcard);
	$view .= form_field_display( $form_tpye . $form_field_idcard   , "Nomor Identitas"    );
	
	
	$datas = array(
		'laki-laki'=>'Laki-laki',
		'perempuan'=>'Perempuan'
	);
	 $kelamin = array(
				'name'=>'kelamin',
				'value'=>( isset($_POST['kelamin']) ? $_POST['kelamin'] : $fields['kelamin'] ),
				'id'=>'kelamin', 
			);
	$form_field_kelamin = form_radiobutton($kelamin , $datas  );
	$view .= form_field_display( $form_field_kelamin   , "Jenis kelamin"    );

	$query = "SELECT * FROM kota ORDER BY nama_kota ASC";
	$result = my_query($query);
	$temps = array();
	while( $r = my_fetch_array($result) ){
		$temps[$r['kota_id']] =$r['nama_kota'];
	}	
	$temp = array(
		'name'=>'tempat_lahir',
		'value'=>( isset($_POST['tempat_lahir'] ) ? $_POST['tempat_lahir'] : $fields['tempat_lahir']  ),
		'id'=>'tempat_lahir'  
	);
	$form_temp = form_dropdown($temp , $temps );
	$view .= form_field_display( $form_temp , "Tempat kelahiran"    );
	
	$tgl_lahir = $fields ? date( 'd-m-Y', strtotime($fields['tanggal_lahir'])) : date('d-m').'-1985';
	$tanggal_lahir = array(
				'name'=>'tanggal_lahir',
				'value'=>( isset($_POST['tanggal_lahir']) ? $_POST['tanggal_lahir'] : $tgl_lahir ),
				'id'=>'tanggal_lahir',
				'type'=>'text','size'=>'55' 
			);
	$form_field_tanggal_lahir = form_calendar ($tanggal_lahir);
	$view .= form_field_display( $form_field_tanggal_lahir   , "Tanggal kelahiran"    );
	 
	$datas = array(
			'Belum Nikah'=>'Belum nikah',
			'Nikah'=>'Nikah',
			'Duda'=>'Duda',
			'Janda'=>'Janda' 
		);
	 $marital = array(
				'name'=>'marital',
				'value'=>( isset($_POST['marital']) ? $_POST['marital'] : $fields['marital'] ),
				'id'=>'marital', 
			);
	$form_field_marital = form_dropdown($marital , $datas);
	$view .= form_field_display( $form_field_marital   , "Status perkawinan"    );
	
	$datasagama = array(
		'islam'=>'Islam',
		'kristen'=>'Kristen',
		'katolik'=>'Katolik',
		'hindu'=>'Hindu' ,
		'budha'=>'Budha' ,
		'lainnya'=>'Lainnya' 
	);
	 $agama = array(
				'name'=>'agama',
				'value'=>( isset($_POST['agama']) ? $_POST['agama'] : $fields['agama'] ),
				'id'=>'agama', 
			);
	$form_field_agama = form_dropdown($agama , $datasagama);
	$view .= form_field_display( $form_field_agama   , "Agama"    );
	$datas = array(
			'O'=>'O',
			'A'=>'A',
			'B'=>'B',
			'AB'=>'AB' 
		);
	 $golda = array(
				'name'=>'gol_da',
				'value'=>( isset($_POST['gol_da']) ? $_POST['gol_da'] : $fields['gol_da'] ),
				'id'=>'gol_da', 
			);
	$form_field_golda = form_dropdown($golda , $datas);
	$view .= form_field_display( $form_field_golda  , "Golongan darah"    );
	 /*
	$telepon_rumah = array(
				'name'=>'telepon_rumah',
				'value'=>( isset($_POST['telepon_rumah']) ? $_POST['telepon_rumah'] : $fields['telepon'] ),
				'id'=>'telepon_rumah',
				'type'=>'text','size'=>'45'
			);
	$form_field_telepon_rumah = form_dynamic($telepon_rumah);
	$view .= form_field_display( $form_field_telepon_rumah , "Telepon rumah"    );
	*/
	$telepon_mobile = array(
				'name'=>'telepon_mobile',
				'value'=>( isset($_POST['telepon_mobile']) ? $_POST['telepon_mobile'] : $fields['telepon_mobile'] ),
				'id'=>'telepon_mobile',
				'type'=>'text','size'=>'45'
			);
	$form_field_telepon_mobile = form_dynamic($telepon_mobile);
	$view .= form_field_display( $form_field_telepon_mobile , "Nomor Telepon "    );
	
	$view .= form_field_display( '&nbsp;' , "<br/><b>Pendidikan terakhir</b>"    );
	
	
	$pendidikans = array();
	$query = "SELECT * FROM pendidikan ORDER BY pendidikan_id ASC";
	$result = my_query($query);
	while($ry = my_fetch_array($result)){
		$pendidikans[$ry['pendidikan_id']] = $ry['label'].' '.$ry['major'];
	}
	$pendidikan = array(
				'name'=>'pendidikan_id',
				'value'=>( isset($_POST['pendidikan_id']) ? $_POST['pendidikan_id'] : $fields['pendidikan_id'] ),
				'id'=>'pendidikan_id',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_pendidikan = form_dropdown($pendidikan , $pendidikans );
	$view .= form_field_display( $form_field_pendidikan , "Nama pendidikan formal"    );
	
	$tahun_pendidikan_a = array(
				'name'=>'tahun_pendidikan_a',
				'value'=>( isset($_POST['tahun_pendidikan_a']) ? $_POST['tahun_pendidikan_a'] : $fields['tahun_a'] ),
				'id'=>'tahun_pendidikan_a',
				'type'=>'text','size'=>'2'
			);
	$form_field_tahun_pendidikan_a = form_dynamic($tahun_pendidikan_a);
	$tahun_pendidikan_b = array(
				'name'=>'tahun_pendidikan_b',
				'value'=>( isset($_POST['tahun_pendidikan_b']) ? $_POST['tahun_pendidikan_b'] : $fields['tahun_b'] ),
				'id'=>'tahun_pendidikan_b',
				'type'=>'text','size'=>'2'
			);
	$form_field_tahun_pendidikan_b = form_dynamic($tahun_pendidikan_b);
	$view .= form_field_display( $form_field_tahun_pendidikan_a.' s/d '.$form_field_tahun_pendidikan_b, "Pada tahun"    );
	
	$no_ijazah = array(
				'name'=>'no_ijazah',
				'value'=>( isset($_POST['no_ijazah']) ? $_POST['no_ijazah'] : $fields['no_ijazah'] ),
				'id'=>'no_ijazah',
				'type'=>'text','size'=>'45'
			);
	$form_field_no_ijazah = form_dynamic($no_ijazah);
	$view .= form_field_display( $form_field_no_ijazah , "No. Ijazah"    );

	$ipk = array(
				'name'=>'ipk',
				'value'=>( isset($_POST['ipk']) ? $_POST['ipk'] : $fields['ipk'] ),
				'id'=>'ipk',
				'type'=>'text','size'=>'5'
			);
	$form_field_no_ipk = form_dynamic($ipk);
	$view .= form_field_display( $form_field_no_ipk , "Nilai Komulatif"    );

	$view .= form_field_display( '&nbsp;' , "<br/><b>Alamat</b>"    );
	$alamat = array(
		'name'=>'alamat',
		'value'=>( isset($_POST['alamat'] ) ? $_POST['alamat']  : $fields['alamat']  ),
		'id'=>'alamat', 
		'cols'=>'45',
		'rows'=>'4'
	);
	$form_field_alamat = form_textarea($alamat);
	$view .= form_field_display( $form_field_alamat   , "Alamat"    );
	 
	$kodepos_data = my_get_data_by_id('kodepos','kodepos_id',( $fields ? $fields['kodepos_id'] : 0 ) );

	$propinsi = array(
		'id' => 'propinsi',
		'name' => 'propinsi',
		'value' => ( isset($_POST['propinsi']) ? $_POST['propinsi'] : strtoupper( $kodepos_data['propinsi'] )),
		'size'	=>'45'
	);
	$form_field_propinsi = form_dynamic($propinsi); 
	$view .= form_field_display( $form_field_propinsi   , "Propinsi"    );
	$kota = array(
		'id' => 'kota',
		'name' => 'kota',
		'value' => ( isset($_POST['kota']) ? $_POST['kota'] : strtoupper( $kodepos_data['kota']) ),
		'size'	=>'45'
	);
	$form_field_kota = form_dynamic($kota);
	$view .= form_field_display( $form_field_kota   , "Kota"    );
	$kecamatan = array(
		'name' => 'kecamatan',
		'id' => 'kecamatan',
		'value' => ( isset($_POST['kecamatan']) ? $_POST['kecamatan'] : strtoupper( $kodepos_data['kecamatan'] )),
		'size'	=>'45'
	);
	$form_field_kecamatan = form_dynamic($kecamatan);
	$view .= form_field_display( $form_field_kecamatan   , "Kecamatan"    );
	
	$kelurahan = array(
		'name' => 'kelurahan',
		'id' => 'kelurahan',
		'value' => ( isset($_POST['kelurahan']) ? $_POST['kelurahan'] : strtoupper( $kodepos_data['kelurahan'] )),
		'size'	=>'35'
	);
	$form_field_kelurahan = form_dynamic($kelurahan);
	$view .= form_field_display( $form_field_kelurahan   , "Kelurahan"    );
	$kodepos = array(
		'name' => 'kodepos',
		'value' => ( isset($_POST['kodepos']) ? $_POST['kodepos'] : $kodepos_data['kodepos'] ),
		'id' => 'kodepos',
		'size'=>'25',
	//	'onchange'=>'javascript:checkKodePos(this.value)' 
	);
	$form_field_kodepos = form_dynamic($kodepos);
	$view .= form_field_display( $form_field_kodepos   , "Kodepos"    );
		
	$view .= form_field_display( '&nbsp;' , "<br/><b>Referensi dan data orang tua</b>"    );
	$referensi = array(
				'name'=>'referensi',
				'value'=>( isset($_POST['referensi']) ? $_POST['referensi'] : $fields['referensi'] ),
				'id'=>'referensi',
				'type'=>'text','size'=>'45'
			);
	$form_field_referensi = form_dynamic($referensi);
	$view .= form_field_display( $form_field_referensi , "Referensi"    );
	 
	$ortu = array(
				'name'=>'ortu',
				'value'=>( isset($_POST['ortu']) ? $_POST['ortu'] : $fields['nama_ortu'] ),
				'id'=>'ortu',
				'type'=>'text','size'=>'45'
			);
	$form_field_ortu = form_dynamic($ortu);
	$view .= form_field_display( $form_field_ortu , "Nama orang tua"    );
	 
	$pekerjaan = array(
				'name'=>'pekerjaan',
				'value'=>( isset($_POST['pekerjaan']) ? $_POST['pekerjaan'] : $fields['kerja_ortu'] ),
				'id'=>'pekerjaan',
				'type'=>'text','size'=>'45'
			);
	$form_field_pekerjaan = form_dynamic($pekerjaan);
	$view .= form_field_display( $form_field_pekerjaan , "Pekerjaan orang tua"    );
	 
	$submit = array(
		'value' => ( $id ==0 ? ' Lanjut ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	$reset = array(
		'value' => 'Reset',
		'name' => '', 
		'type'=>'reset','class'=>'main_button'
	);
	$form_reset= form_dynamic($reset);

	$back = array(
		'value' => ' Kembali ',
		'name' => 'back', 
		'type'=>'button',
		'onclick'=>'location.href=\'index.php?com=pelamar\'',
		'class'=>'main_button'
	);
	$form_back = form_dynamic($back); 
	
	$view .= form_field_display( $form_submit .' '.$form_reset.' '.$form_back , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_validate($id =0){
	$errsubmit = false;
	$err = array();
	$code =  trim($_POST["nama"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Nama belum di isi";
	}
	$code =  trim($_POST["kelamin"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Kelamin belum di isi";
	}

	if(!is_numeric($_POST['tahun_pendidikan_a']) ){
		$errsubmit = true;
		$err[] = "Tahun harus angka" ;
	}
	if(!is_numeric($_POST['tahun_pendidikan_b']) ){
		$errsubmit = true;
		$er[] = "Tahun harus angka" ;
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function form_submit($id =0){
	list($dd,$mm,$yyyy) = explode("-" , $_POST['tanggal_lahir']);
	$tanggal = $yyyy .'-'. $mm . '-' . $dd ;
	$datas = array(
		'idcard'=>my_type_data_str($_POST['tno'].'#'.strtoupper($_POST['idcard'])),
		'nama'=>my_type_data_str($_POST['nama']),
		'kelamin'=>my_type_data_str($_POST['kelamin']),
		'tempat_lahir'=>my_type_data_int($_POST['tempat_lahir']),
		'tanggal_lahir'=>my_type_data_str($tanggal),
		'agama'=>my_type_data_str($_POST['agama']),
		'gol_da'=>my_type_data_str($_POST['gol_da']),
		'email'=>my_type_data_str($_POST['email']),
		'telepon_mobile'=>my_type_data_str($_POST['telepon_mobile']),
		'marital'=>my_type_data_str($_POST['marital']),
		'datetime_added'=>my_type_data_function('NOW()')
 
	);
	if( $id > 0 ){
		$ck = my_get_data_by_id('pelamar','pelamar_id',$id);
		$biodata_id = $ck['biodata_id'] ;
		my_update_record('biodata' , 'biodata_id' , $biodata_id , $datas );
	}else{	
		$biodata_id = my_insert_record('biodata' , $datas );
	}	
	
  	list($ddt,$mmt,$yyyyt) = explode("-" , $_POST['tanggal_terima']);
	$tanggal_terima =$yyyyt .'-'. $mmt . '-' . $ddt ; 
	$pelamar_datas =  array(
		'biodata_id'=>my_type_data_int($biodata_id), 
		'pendidikan_id'=>my_type_data_int($_POST['pendidikan_id']), 
		'tanggal_terima'=>my_type_data_str($tanggal_terima), 
		'referensi'=>my_type_data_str($_POST['referensi']), 
		'nama_ortu'=>my_type_data_str($_POST['ortu']), 
		'kerja_ortu'=>my_type_data_str($_POST['pekerjaan']), 
		'nomor_agenda'=>my_type_data_str($_POST['nomorbaru'] ) ,
		'no_ijazah' 	=>my_type_data_str($_POST['no_ijazah'] ) ,
		'ipk' 	=>my_type_data_str($_POST['ipk'] ) ,
		'tahun_a' =>my_type_data_str($_POST['tahun_pendidikan_a'] ) ,	
		'tahun_b'=>my_type_data_str($_POST['tahun_pendidikan_b'] ) ,
		'datetime_added'=>my_type_data_function('NOW()')
	);	
	if( $id > 0 ){
		my_update_record('pelamar' , 'pelamar_id' ,  $id , $pelamar_datas );
		$pelamar_id = $id ;
	}else{	
		$pelamar_id = my_insert_record('pelamar' , $pelamar_datas );
	}	
	$kodepos_data = my_get_data_by_id('kodepos','kodepos','"'.$_POST['kodepos'].'"' );
	$kodepos_id = isset( $kodepos_data['kodepos_id'] ) ? $kodepos_data['kodepos_id'] :  0;

	$datas_pelamar = array(
		'pelamar_id'=>my_type_data_int($pelamar_id), 
		'alamat'=>my_type_data_str($_POST['alamat']),
		'kodepos_id'=>my_type_data_int($kodepos_id),
		'telepon'=>my_type_data_str($_POST['telepon_rumah']),
		'type'=>my_type_data_str('tinggal'	),
	);	
	my_delete_record(	'pelamar_alamat',	'pelamar_id',	$pelamar_id);
	my_insert_record('pelamar_alamat' , $datas_pelamar );
	my_direct('index.php?com=pelamar');
}


//	================================  SELEKSI PELAMAR ==========================

function report_calon_karyawan_seleksi(){
	$header = array( 
		'pic'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'usia'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'no agenda'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),
		'ujian terakhir'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'tanggal ujian'=>array('style'=>'text-align:center;border-bottom:2px solid;width:13%'),
		'hasil'=>array('style'=>'border-bottom:2px solid;width:5%'),  
	//	'ptk'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
	);
	$query = "
	SELECT * FROM pelamar a 
	INNER JOIN biodata b ON a.biodata_id=b.biodata_id
	INNER JOIN pelamar_seleksi c ON a.pelamar_id=c.pelamar_id	
	GROUP BY a.pelamar_id
	ORDER BY  c.pelamar_seleksi_id DESC 
	";
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=pelamar&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$ujians=get_ujian_terakhir($ey['pelamar_id']);
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=pelamar&task=edit_ptk&pelamar_id='.$ey['pelamar_id'],
						'rel'=>'facebox',
						'title'=>'PTK'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$detailproperty = array(
						'href'=>'index.php?com=pelamar&task=detail&id='.$ey['pelamar_id'],						 
						'title'=>'Proses ujian'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$ptk_id = isset( $ey['ptk_id'] ) ? $ey['ptk_id']:  0;
		$ptks=my_get_data_by_id('ptk','ptk_id',$ptk_id); 
		$row[] = array( 
			'photo'=>position_text_align( '<img src="files/upload/sample_thumb.jpg" height="30" width="30" />', 'center'),
			'nama'=>$ey['nama'], 
			'usia'=>position_text_align(  date('Y') - date('Y' , strtotime($ey['tanggal_lahir'] ." 00:00:01") ) , 'center'),
			'no peserta'=>position_text_align( $ey['nomor_agenda'], 'center'), 
			'ujian'=> $ujians['nama_seleksi_pelamar'],  
			'tgl'=>position_text_align(  ($ey['tanggal_panggil'] <>'0000-00-00') ?date('d- m - Y' , strtotime($ey['tanggal_panggil']  )) : '', 'center'), 
			'hasil'=> $ujians['hasil'],  
		//	'ptk'=> $ptks['nomor'],   
			'operasi'=> position_text_align( /*$edit_button.' '.*/ $detail_button   , 'right'),  
		);
	}
	
	$datas = table_rows($row); 
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel\';'),
	);	
	$printmenu = additional_menu_on_list( $menuontop , $search  );	
	return  $printmenu.table_builder($header , $datas ,  9 ,false ) ;
}


// ================================== edit PTK ================================
function edit_ptk($pelamar_id){
	
	$view = form_header( "edit_ptk" , "edit_ptk"  );
	$fields = my_get_data_by_id('pelamar','pelamar_id',$pelamar_id);
	$ptks = array();
	$query = "SELECT nomor,ptk_id FROM ptk 
		ORDER BY nomor ";
	 $re = my_query($query);
	 while($row = my_fetch_array($re) ){
		$ptks[$row['ptk_id']] =$row['nomor'];
	 }
	 $form = array(
		'name'=>'ptk_id',
		'id'=>'ptk_id',
		'value'=>isset($_POST['ptk_id']) ? $_POST['ptk_id'] : $fields['ptk_id'] 
		);
	$form_temp = form_dropdown($form , $ptks);
	$view .= form_field_display( $form_temp  , "Nomor PTK"  );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function edit_ptk_submit($pelamar_id){
	$datas = array(
			'ptk_id'=>my_type_data_int($_POST['ptk_id']), 	
		);
	if($pelamar_id > 0){
		return my_update_record('pelamar','pelamar_id',$pelamar_id, $datas);
	}
}

function  edit_ptk_validate($pelamar_id){

	$errsubmit = false;
	$err = array();

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

// ========== DITERIMA ====
function seleksi_pelamar_diterima($pelamar_id){
	$pelamar 		= my_get_data_by_id('pelamar','pelamar_id',$pelamar_id);
	$date_penerimaan = get_tanggal_lulus_wawancara($pelamar_id);
	$karyawan_id 	= insert_new_karyawan_by_biodata( $pelamar['biodata_id'] ,$pelamar['nomor_agenda'] ,$date_penerimaan  );
	$datas = array(
		'karyawan_id' => my_type_data_int($karyawan_id),
		'pelamar_id' => my_type_data_int($pelamar_id),
	);
	return my_insert_record('karyawan_disable' , $datas);
}

function get_tanggal_lulus_wawancara($pelamar_id){
	$query = "
		SELECT tanggal_panggil FROM pelamar_seleksi 
		WHERE seleksi_pelamar_id = 5 AND pelamar_id = {$pelamar_id}
		ORDER by pelamar_seleksi_id DESC 
		";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['tanggal_panggil'];
}

function insert_new_karyawan_by_biodata( $biodata_id , $nomor_agenda , $tanggal_kontrak ){
	$datas = array(
		'biodata_id'=>my_type_data_int( $biodata_id ),
		'nomor_kontrak'=>my_type_data_str($nomor_agenda ),
		'tanggal_kontrak'=>my_type_data_str( $tanggal_kontrak ),
	);
	 
	return my_insert_record('karyawan',$datas);
}

//	================================  SELEKSI PELAMAR DETAIL ==========================

function pelamar_ujian_progress($pelamar_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete_setuji&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'seleksi'=>array('style'=>'text-align:left;border-bottom:2px solid;width:55%'), 
		'status'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'), 
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'), 
	);
	$query = "SELECT * FROM pelamar_seleksi WHERE pelamar_id ={$pelamar_id}";
	$result =my_query($query);
	$ujians=get_ujian_terakhir($pelamar_id);	
	$row=array();
	while( $ey = my_fetch_array($result) ){
		$seleksi_pelamars=my_get_data_by_id('seleksi_pelamar','seleksi_pelamar_id',$ey['seleksi_pelamar_id']);
		if($ujians['seleksi_pelamar_id']==$ey['seleksi_pelamar_id']){
			$editproperty = array(
							'href'=>'index.php?com='.$_GET['com'].'&task=setuji&pelamar_id='.$pelamar_id.'&pelamar_seleksi_id='.$ey['pelamar_seleksi_id'],
							'rel'=>'facebox',
							'title'=>'Edit'
					);
			$edit_button = button_icon( 'b_edit.png' , $editproperty  );
			
			$deleteproperty = array(
							'href'=>'javascript:; ',
							'onclick'=>'javascript:confirmDelete('.$ey['pelamar_seleksi_id'].');',
							'title'=>'Delete'
					);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}else{	
			$edit_button = ' ';
			$delete_button = ' ';
		}	
		$row[] = array( 
			'tanggal_panggil'=>position_text_align( date('d-m-Y' , strtotime($ey['tanggal_panggil'])),'center'), 
			'kategori_seleksi'=>  $seleksi_pelamars['nama_seleksi_pelamar']  ,
			'hasil'=>  ucfirst($ey['hasil']),   
			'operasi'=> position_text_align( $edit_button.' '. $delete_button , 'right'),  
		);
	}
	$datas = table_rows($row); 
	$pelamar = my_get_data_by_id('pelamar','pelamar_id',$pelamar_id);
	$biodata = my_get_data_by_id('biodata','biodata_id',$pelamar['biodata_id']);
	$view = form_header( "pelamar" , "cp"  );
	$view .= form_field_display( $biodata['nama'] , "Nama pelamar"    ); 
	$view .= form_field_display( $pelamar['nomor_agenda'] , "Nomor agenda"    ); 
	if(trim($pelamar['referensi']) <> '')
	$view .= form_field_display( $pelamar['referensi']  , "Referensi"    ); 
	$form_diterima=''; $form_tambah='';
	if(check_diterima_button($pelamar_id)){
		$temp = array(
			'value' => ' Diterima ',
			'name' => 'diterima', 
			'type'=>'button',
			'onclick'=>'location.href=\'index.php?com=pelamar&task=diterima&pelamar_id='.$pelamar_id.'\'',
			'class'=>'main_button'
		);
		$form_diterima = form_dynamic($temp); 
	}else{
	 
		if($ujians['hasil']=="lulus"){		
			$temp = array(
				'value' => 'Tambah ',
				'name' => 'tambah', 
				'type'=>'button',
				'onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pelamar&task=setuji&pelamar_id='.$pelamar_id.'&pelamar_seleksi_id=0\' })',
				'class'=>'main_button'
			);
			$form_tambah = form_dynamic($temp); 
		}
	}
	$back = array(
		'value' => ' Kembali ',
		'name' => 'back', 
		'type'=>'button',
		'onclick'=>'location.href=\'index.php?com=pelamar&task=seleksi\'',
		'class'=>'main_button'
	);
	$form_back = form_dynamic($back); 
	$view .= form_field_display( $form_diterima  .' '. $form_tambah .' '. $form_back , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
/*	
	$menuontop = array() ;
	if(check_diterima_button($pelamar_id)){
		$menuontop[] = array('Diterima'=>array('onclick'=>'javascript:location.href=\'index.php?com=pelamar&task=diterima&pelamar_id='.$pelamar_id.'\''));   
	}
	if($ujians['hasil']=="lulus"){		
		$menuontop[] = array( 'Tambah data'=>array('onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pelamar&task=setuji&pelamar_id='.$pelamar_id.'&pelamar_seleksi_id=0\' })'));
	}
	$menuontop[] = array('Back'=>array('onclick'=>'javascript:location.href=\'index.php?com=pelamar&task=seleksi\''));
*/
	//$printmenu = additional_menu_on_list( $menuontop,"");
	return $view.table_builder($header , $datas ,  7 ,false );
}

function check_diterima_button($pelamar_id){
	$query = "SELECT * FROM pelamar_seleksi 
		WHERE pelamar_id ={$pelamar_id} 
		AND seleksi_pelamar_id =5 
		AND hasil='lulus' ";
	$res = my_query($query);
	if( my_num_rows($res) > 0 ){
		return true;
	}
	return false;
}


function pelamar_ujian($pelamar_id,$pelamar_seleksi_id){
	my_set_file_js(array('components/system/js/calendar/calendarDateInput.js'));
	$view = form_header( "calon karyawan" , "cp"  );
 	$fields = my_get_data_by_id(	'pelamar_seleksi',	'pelamar_seleksi_id', $pelamar_seleksi_id);

	$tanggal_terima = array(
			'name'=>'tanggal',
			'value'=>( isset($_POST['tanggal']) ? $_POST['tanggal'] : ($fields ? date('d-m-Y',strtotime($fields['tanggal_panggil']) ): date('d-m-Y') )),
			'id'=>'tanggal',
			'type'=>'text' 
		);
	$form_field_tanggal_lahir = form_calendar ($tanggal_terima);
	$pelamar = my_get_data_by_id('pelamar','pelamar_id',$pelamar_id);
	$bio = my_get_data_by_id('biodata','biodata_id',$pelamar['biodata_id']);
	$view .= form_field_display( $bio['nama']   , "Nama"    );
	$view .= form_field_display( $pelamar['nomor_agenda']   , "Nomor agenda"    );
	$view .= form_field_display( $form_field_tanggal_lahir   , "Tanggal ujian"    );
	
	if($fields){
		$seleksi_pelamar_id=$fields['seleksi_pelamar_id'];
	}else{
		$ujians=get_ujian_terakhir($pelamar_id);	
		$seleksi_pelamar_id=$ujians['seleksi_pelamar_id']+1 ;
	}
	$seleksi_pelamars = my_get_data_by_id(	'seleksi_pelamar',	'seleksi_pelamar_id', $seleksi_pelamar_id);

	$temp = array(
				'name'=>'seleksi_pelamar_id',
				'value'=>$seleksi_pelamar_id,
				'id'=>'seleksi_pelamar_id',
				'type'=>'hidden',
			);
	$form_temp= form_dynamic($temp);
	$view .= form_field_display( $seleksi_pelamars['nama_seleksi_pelamar'] .$form_temp , "Seleksi"    ); 	

	$datas_status = array(
		'lulus'=>'Lulus',
		'tidak lulus'=>'Tidak lulus',
		 );
	$jenis_status = array(
		'name'=>'hasil',
		'id'=>'hasil',
		'value'=>(isset($_POST['hasil']) ? $_POST['hasil']: $fields['hasil']),
	);
	$status= form_dropdown($jenis_status,$datas_status);
	$view .= form_field_display( $status   , "Hasil"    );
	
	$s = array(
		'value' => 'Submit',
		'name' => '', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_s= form_dynamic($s);
	
	$view .= form_field_display( $form_s , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function pelamar_ujian_submit($pelamar_id ,$pelamar_seleksi_id){
	
	list($dd,$mm,$yyyy) = explode('-',$_POST['tanggal']);
	$tanggal = $yyyy.'-'.$mm.'-'.$dd;
 
	$datas = array(
		'pelamar_id'	=> my_type_data_int($pelamar_id),
		'tanggal_panggil'=> my_type_data_str($tanggal),
		'seleksi_pelamar_id'=> my_type_data_int($_POST['seleksi_pelamar_id']), 
		'hasil'=> my_type_data_str($_POST['hasil']), 
	); 
	
	if($pelamar_seleksi_id > 0){
		return my_update_record('pelamar_seleksi' , 'pelamar_seleksi_id' , $pelamar_seleksi_id ,$datas);
	}
	return my_insert_record('pelamar_seleksi',$datas);
}


function loaddata_pelamar($pelamar_id){
	$query = "SELECT * FROM pelamar a
	INNER JOIN pelamar_alamat b ON a.pelamar_id = b.pelamar_id
	INNER JOIN biodata c ON a.biodata_id=c.biodata_id WHERE a.pelamar_id = {$pelamar_id}";
	$result =my_query($query);
	return my_fetch_array($result);
}

function form_detail_calon_karyawan_id($id){
	$query  = "
SELECT * ,b.nama AS nam FROM pelamar a
INNER JOIN biodata b ON a.biodata_id = b.biodata_id
INNER JOIN pelamar_alamat c ON a.pelamar_id = c.pelamar_id
WHERE a.pelamar_id = {$id}
	";
	$result = my_query($query);
	$fields =  my_fetch_array($result); 
	$view  = form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.ucfirst($fields['nam']).'</span>'   , "<b>Nama pelamar</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['nomor_agenda'] .'</span>'  , "<b>Nomor agenda</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['idcard'] .'</span>'  , "<b>Nomor identitas</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['email'] .'</span>'  , "<b>Email</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($fields['kelamin']) .'</span>'  , "<b>Jenis kelamin</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($fields['tempat_lahir'] ) .', '.   date('d M Y',strtotime($fields['tanggal_lahir'].' 00:00:01')).'</span>' , "<b>Tempat, tanggal lahir</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['marital'] .'</span>'  , "<b>Status perkawinan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['agama']   .'</span>'  , "<b>Agama</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['pendidikan']   .'</span>'  , "<b>Pendidikan terakhir</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.nl2br($fields['alamat'] ).'</span>'  , "<b>Alamat</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($fields['kota'] ).'</span>'  , "<b>Kota</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['telepon'] .'</span>'  , "<b>Nomor telepon rumah</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$fields['telepon_mobile'].'</span>'  , "<b>Nomor telepon seluler</b>"    ); 

	$submit = array(
		'value' => ( 'Kembali'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=(\'index.php?com=pelamar\')',
		'class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit   , "&nbsp;" ,  "" );	
	$view .= form_footer( ); 
	
	return $view;
}

function get_ujian_terakhir($pelamar_id){
	$query = "SELECT a.seleksi_pelamar_id as seleksi_pelamar_id, b.nama_seleksi_pelamar  , a.hasil FROM pelamar_seleksi a 
	inner join seleksi_pelamar b on a.seleksi_pelamar_id=b.seleksi_pelamar_id 	
	WHERE a.pelamar_id = {$pelamar_id} 
	ORDER BY seleksi_pelamar_id desc limit 1";
	$result =my_query($query);
	return my_fetch_array($result);
}


function print_plain(){
	$query = "
	SELECT * FROM pelamar a 
	INNER JOIN biodata b ON a.biodata_id=b.biodata_id
	INNER JOIN pelamar_seleksi c ON a.pelamar_id=c.pelamar_id	
	GROUP BY a.pelamar_id
	ORDER BY  c.pelamar_seleksi_id DESC 
	";
	$result = my_query($query ); 
	$datas = array();  

	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_BOTH),
		 'k2'=>array("Nama" ,20,STR_PAD_RIGHT),
		 'k3'=>array("Usia" ,5,STR_PAD_RIGHT),
		 'k4'=>array("No Agenda" ,10,STR_PAD_RIGHT),  
		 'k5'=>array("Tanggal Ujian" ,20,STR_PAD_BOTH),  
		 'k6'=>array("Ujian Terakhir" ,15,STR_PAD_BOTH),  
		 'k7'=>array("Hasil" ,10,STR_PAD_BOTH),  
		 'k8'=>array("PTK" ,15,STR_PAD_BOTH),  
	); 

	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_BOTH),
		 'k2'=>array(" " ,20,STR_PAD_RIGHT),
		 'k3'=>array(" " ,5,STR_PAD_RIGHT),
		 'k4'=>array(" " ,10,STR_PAD_RIGHT),  
		 'k5'=>array(" " ,20,STR_PAD_BOTH),  
		 'k6'=>array(" " ,15,STR_PAD_BOTH),  
		 'k7'=>array(" " ,10,STR_PAD_BOTH),  
		 'k8'=>array(" " ,15,STR_PAD_BOTH),  
	); 

	$i=0;
	while($ey = my_fetch_array($result)){
		$ujians=get_ujian_terakhir($ey['pelamar_id']);	
		$ptk_id = isset( $ey['ptk_id'] ) ? $ey['ptk_id']:  0;
		$ptks=my_get_data_by_id('ptk','ptk_id',$ptk_id); 
		$i++; 		
		$datas[] = array(		 
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array($ey['nama'],20,STR_PAD_RIGHT),
			'k3'=>array(date('Y') - date('Y' , strtotime($ey['tanggal_lahir'] ." 00:00:01") ) ,5,STR_PAD_RIGHT),
			 'k4'=>array($ey['nomor_agenda'] ,10,STR_PAD_RIGHT),  
			 'k5'=>array(($ey['tanggal_panggil'] <>'0000-00-00') ?date('d- m - Y' , strtotime($ey['tanggal_panggil']  )) : '' ,20,STR_PAD_BOTH),  
			 'k6'=>array( $ujians['nama_seleksi_pelamar'],15,STR_PAD_BOTH),  
			 'k7'=>array( $ujians['hasil'] ,10,STR_PAD_BOTH),  
			 'k8'=>array( $ptks['nomor'] ,15,STR_PAD_BOTH),  
		);
	}
	$view = 	str_pad("CETAK SELEKSI PELAMAR",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}

function excel_report(){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'nama'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'usia'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
		'no agenda'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),
		'tanggal ujian'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),
		'ujian terakhir'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'hasil'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		//'ptk'=>array('style'=>'border-bottom:2px solid;width:20%'),  
	);
	$query = "
	SELECT * FROM pelamar a 
	INNER JOIN biodata b ON a.biodata_id=b.biodata_id
	INNER JOIN pelamar_seleksi c ON a.pelamar_id=c.pelamar_id	
	GROUP BY a.pelamar_id
	ORDER BY  c.pelamar_seleksi_id DESC 
	";
	$result = my_query($query ); 
	$row = array();  
	$i=1;
	while($ey = my_fetch_array($result)){
		$ujians=get_ujian_terakhir($ey['pelamar_id']);	
		$ptk_id = isset( $ey['ptk_id'] ) ? $ey['ptk_id']:  0;
		$ptks=my_get_data_by_id('ptk','ptk_id',$ptk_id); 
		$row[] = array( 
			'#'=>position_text_align ($i, 'center'),		
			'nama'=>$ey['nama'], 
			'usia'=>position_text_align(  date('Y') - date('Y' , strtotime($ey['tanggal_lahir'] ." 00:00:01") ) , 'center'),
			'no peserta'=>position_text_align( $ey['nomor_agenda'], 'center'), 
			'tgl'=>position_text_align(  ($ey['tanggal_panggil'] <>'0000-00-00') ?date('d- m - Y' , strtotime($ey['tanggal_panggil']  )) : '', 'center'), 
			'ujian'=> $ujians['nama_seleksi_pelamar'],  
			'hasil'=> $ujians['hasil'],  
		//	'ptk'=> $ptks['nomor'],   
		);
		$i++;
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 9 ,false ); 
}

function detail_pelamar($pelamar){
	if(! is_array($pelamar))return false;
	$photo = my_get_data_by_id('photo','biodata_id',$pelamar['biodata_id']);
	$biodata = my_get_data_by_id('biodata','biodata_id',$pelamar['biodata_id']);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr><td width="15%" align="center" style="background:#CDCDCD" valign="top">';
	if(file_exists($photo['upload_path']))
		$view .= '<img src="'.$photo['upload_path'].'" width="120px" height="160px"/><br/>';
	else
		$view .= '<img src="files/upload/sample_thumb.jpg" width="120px" height="160px"/><br/>';
		
	$view_admin  = '<a href="index.php?com='.$_GET['com'].'&task=ganti_photo&id='.$pelamar['biodata_id'].'" rel="facebox" style="font:10px verdana;">[Ganti photo]</a>	';
	if(is_admin() )$view .=$view_admin;
	$view .='</td><td width="85%" align="left" valign="bottom">';
	$view .=  form_header_no_post( "detail pelamar" , "cp"  );
	$view .= form_field_display( '<span class="label_form">: '. $pelamar['nomor_agenda']   .'</span>'  , "<b>Nomor agenda</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '.date('d M Y',strtotime($pelamar['tanggal_terima'] )  ) .'</span>'  , "<b>Tanggal terima surat</b>"    );  
	
	$view .= form_field_display( '<span class="label_form">: '.ucfirst($biodata['nama'] )  .'</span>' , "<b>Nama pelamar</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '.$biodaat['email']    .'&nbsp;</span>' , "<b>Email</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '.$biodata['idcard']    .'</span>' , "<b>Nomor identitas</b>"    ); 
	
	$view .= form_field_display( '<span class="label_form">: '.ucfirst($biodata['kelamin'] ) .'</span>'     , "<b>Jenis kelamin</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '.ucfirst($biodata['marital'] ) .'</span>'     , "<b>Status perkawinan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '.ucfirst($biodata['agama'] ) .'</span>'     , "<b>Agama</b>"    ); 
	 
	$view .='<tr><td colspan="3"><br/>&nbsp;<br /><b>PENDIDIKAN TERAKHIR</b></td></tr>';
	$pendidikan = my_get_data_by_id('pendidikan','pendidikan_id',$pelamar['pendidikan_id']);
	$view .= form_field_display( '<span class="label_form">: '.ucfirst($pendidikan['code'] ) .'  </span>'     , "<b>&nbsp; Nama pendidikan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '. $pelamar['tahun_a'] .' &nbsp; s/d &nbsp; '.$pelamar['tahun_b']  .'  </span>'     , "<b>&nbsp; Tahun</b>"    ); 
	$view .= form_field_display( '<span class="label_form">: '. $pelamar['no_ijazah']  .' / '. $pelamar['ipk']  .' </span>'     , "<b>&nbsp; No Ijazah / IPK</b>"    ); 
	 
	
	$view .= form_footer( ); 
	$view .= '</td></tr></table>';
	
	$view .= list_keahlian($pelamar['pelamar_id']);
	$view .= list_organisasi($pelamar['pelamar_id']);
	return $view;
}

function list_organisasi($pelamar_id){
	my_set_code_js('
		function confirmDeleteOrg(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com=pelamar&task=delete_org&pel_org_id=\'+i;
		}
		return;	
		}
	');
	$header = array(
	 	'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
	 	'Nama organisasi'=>array('style'=>'text-align:left;border-bottom:2px solid;width:45%'), 
	 	'Tahun'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	 	'Jabatan terakhir'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'), 
		  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	);
	$query = "
	 SELECT * FROM pelamar_organisasi WHERE pelamar_id = {$pelamar_id}
	";
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=pelamar&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;  
		$deleteproperty = array(
				'href'=>'javascript:;',
				'onclick'=>'javascript:confirmDeleteOrg('.$ey['pelamar_org_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			 
		$row[] = array(
		 	'#'=>position_text_align ($i, 'center'), 
			'nama'=>$ey['nama_organisasi'],  
			'tahun'=>position_text_align($ey['tahun'], 'center'), 
			'jabatan'=>position_text_align($ey['jabatan_terakhir'], 'left'), 
			'operasi'=> position_text_align(  $edit_button.' '. $detail_button.' '.$delete_button , 'right'),  
		);
	}
	
	$datas = table_rows($row);  
	$menuontop = array(   
		 
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=create_organisasi&pelamar_id='.$pelamar_id.'\' });'), 
	);
	$namearea = '<br/>&nbsp;<br/><b>PENGALAMAN ORGANISASI</b>';
	$printmenu = additional_menu_on_list( $menuontop , $namearea ); 
	return $printmenu. table_builder($header , $datas ,  7 ,false ); 
}

function create_organisasi_pelamar($pelamar_id){
	$view = '';
	$view .=  form_header( "detail pelamar" , "cp"  );
	
	$temp = array(
				'name'=>'nama_organisasi',
				'value'=>'',
				'id'=>'nama_organisasi',
				'type'=>'text', 
			);
	$form_nama_org= form_dynamic($temp); 	
	$view .= form_field_display(   $form_nama_org  , "<b>Nama organisasi</b>"    ); 
	
	$tahun = array(
				'name'=>'tahun',
				'value'=>(date('Y') - 3 ),
				'id'=>'tahun', 
			);
	$tahuns = array();
	for($yr = date('Y'); $yr>=1998; $yr-- ){
		$tahuns[$yr] = $yr;
	}
	$form_year= form_dropdown($tahun , $tahuns); 	
	$view .= form_field_display(  $form_year   , "<b>Tahun</b>"    ); 
	$jabatan_terakhir = array(
				'name'=>'jabatan_terakhir',
				'value'=>'',
				'id'=>'jabatan_terakhir',
				'type'=>'text', 
			);
	$form_jabatan_terakhir= form_dynamic($jabatan_terakhir); 
	$view .= form_field_display(   $form_jabatan_terakhir  , "<b>Jabatan Terakhir</b>"    ); 
	$submit = array(
		'value' => ( 'Tambahkan'),
		'name' => 'simpan', 
		'type'=>'submit', 
		'class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display(   $form_submit  , "&nbsp;"    ); 
	$view .= form_footer( ); 
	return $view;
}

function create_organisasi_pelamar_submit($pelamar_id){
	$datas = array(
		'nama_organisasi'	=> my_type_data_str($_POST['nama_organisasi']),
		'jabatan_terakhir'	=> my_type_data_str($_POST['jabatan_terakhir']),
		'tahun'	=> my_type_data_str($_POST['tahun']),
		'pelamar_id'	=> my_type_data_str($pelamar_id),
	);
	return my_insert_record('pelamar_organisasi',$datas);
}


function list_pengalaman_kerja($pelamar_id){
	
}

function list_keahlian($pelamar_id){
my_set_code_js('
		function confirmDeleteAhli(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com=pelamar&task=delete_ahli&pel_org_id=\'+i;
		}
		return;	
		}
	');
	$header = array(
	 	'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
	 	'Nama keahlian'=>array('style'=>'text-align:left;border-bottom:2px solid;width:45%'), 
	 	'Kemampuan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	 	'Keterangan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'), 
		  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	);
	$query = "
	 SELECT * FROM pelamar_keahlian WHERE pelamar_id = {$pelamar_id}
	";
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=pelamar&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;  
		$deleteproperty = array(
				'href'=>'javascript:;',
				'onclick'=>'javascript:confirmDeleteAhli('.$ey['pelamar_org_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			 
		$row[] = array(
		 	'#'=>position_text_align ($i, 'center'), 
			'nama_keahlian'=>$ey['nama_keahlian'],  
			'level'=>position_text_align($ey['tingkat'], 'center'), 
			'jabatan'=>position_text_align($ey['keterangan'], 'left'), 
			'operasi'=> position_text_align(  $edit_button.' '. $detail_button.' '.$delete_button , 'right'),  
		);
	}
	
	$datas = table_rows($row);  
	$menuontop = array(   
		 
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=create_keahlian&pelamar_id='.$pelamar_id.'\' });'), 
	);
	$namearea = '<br/>&nbsp;<br/><b>KEAHLIAN KHUSUS</b>';
	$printmenu = additional_menu_on_list( $menuontop , $namearea ); 
	return $printmenu. table_builder($header , $datas ,  7 ,false ); 
}

function create_keahlian($pelamar_id){
	$view = '';
	$view .=  form_header( "detail pelamar" , "cp"  );
	
	$nama_keahlian = array(
				'name'=>'nama_keahlian',
				'value'=>'',
				'id'=>'nama_keahlian',
				'type'=>'text', 
			);
	$form_nama_keahlian= form_dynamic($nama_keahlian); 	
	$view .= form_field_display(   $form_nama_keahlian  , "<b>Nama/Jenis keahlian</b>"    ); 
	
	$tingkat = array(
				'name'=>'tingkat',
				'value'=>'',
				'id'=>'tingkat', 
			);
	$tingkats = array(
		'Bisa'=>'Bisa',
		'Cukup'=>'Cukup',
		'Mahir'=>'Mahir',
	);
	 
	$form_tingkat = form_dropdown($tingkat , $tingkats); 	
	$view .= form_field_display(  $form_tingkat   , "<b>Tingkat keahlian</b>"    ); 
	
	$keterangan = array(
				'name'=>'keterangan',
				'value'=>'',
				'id'=>'keterangan', 
				'rows'=>'5'
			);
	$form_keterangan= form_dynamic($keterangan); 
	$view .= form_field_display(   $form_keterangan  , "<b>Keterangan</b>"    ); 
	$submit = array(
		'value' => ( 'Tambahkan'),
		'name' => 'simpan', 
		'type'=>'submit', 
		'class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display(   $form_submit  , "&nbsp;"    ); 
	$view .= form_footer( ); 
	return $view;
}

function create_keahlian_submit($pelamar_id ){
	$datas = array(
		'nama_keahlian'	=> my_type_data_str($_POST['nama_keahlian']),
		'keterangan'	=> my_type_data_str($_POST['keterangan']),
		'tingkat'	=> my_type_data_str($_POST['tingkat']),
		'pelamar_id'	=> my_type_data_str($pelamar_id),
	);
	return my_insert_record('pelamar_keahlian',$datas);
} 

function nomer_pelamar_baru(){
	$query = "SELECT RIGHT(nomor_agenda,4) as newno FROM pelamar WHERE YEAR(tanggal_terima) = YEAR(NOW()) ORDER BY pelamar_id DESC limit 1";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$newnumber =  date('Ym') . sprintf("%04.0f",  (int)$row['newno']+1);
	return  $newnumber;
	 
}